package com.example.nick.bodyapplication.anim

import android.content.Context
import android.graphics.drawable.Drawable
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.StateListDrawable
import android.graphics.drawable.shapes.RoundRectShape
import android.graphics.drawable.shapes.Shape
import android.util.AttributeSet
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.example.nick.bodyapplication.R

class LoginText(context: Context, attrs: AttributeSet?) : androidx.appcompat.widget.AppCompatTextView(context, attrs) {

    override fun setBackgroundColor(color: Int) {
//        super.setBackgroundColor(color)
        val bg = generateRoundDrawable(height.toFloat(), color, color)
        setBackgroundDrawable(bg)
    }

    fun generateRoundDrawable(radii: Float, pressColor: Int, defaultColor: Int): Drawable? {
        //圆角
        val roundRectShape: Shape = RoundRectShape(
            floatArrayOf(radii, radii, radii, radii, radii, radii, radii, radii),
            null,
            null
        ) //圆角背景

        //按下状态
        val shopDrawablePress = ShapeDrawable(roundRectShape) //圆角shape
        shopDrawablePress.paint.color = pressColor //设置颜色

        //正常状态
        val shopDrawableNormal = ShapeDrawable(roundRectShape)
        shopDrawableNormal.paint.color = defaultColor
        val bgStateDrawable = StateListDrawable() //状态shape
        bgStateDrawable.addState(intArrayOf(android.R.attr.state_pressed), shopDrawablePress) //按下状态
        bgStateDrawable.addState(intArrayOf(-android.R.attr.state_enabled), shopDrawablePress)
        bgStateDrawable.addState(intArrayOf(), shopDrawableNormal) //其他状态
        return bgStateDrawable
    }
}